/*     */ package cc.s2m.web.s2mBlog.task;
/*     */ 
/*     */ import cc.s2m.web.s2mBlog.pojo.Articles;
/*     */ import cc.s2m.web.s2mBlog.pojo.ArticlesContent;
/*     */ import cc.s2m.web.s2mBlog.pojo.ArticlesTags;
/*     */ import cc.s2m.web.s2mBlog.pojo.SysConfig;
/*     */ import cc.s2m.web.s2mBlog.service.IArticles;
/*     */ import cc.s2m.web.s2mBlog.service.IArticlesContent;
/*     */ import cc.s2m.web.s2mBlog.service.IArticlesTags;
/*     */ import cc.s2m.web.s2mBlog.service.ISysConfig;
/*     */ import cc.s2m.web.s2mBlog.util.MemcacheKeys;
/*     */ import cc.s2m.web.s2mBlog.util.StaticProp;
/*     */ import java.io.File;
/*     */ import java.net.URL;
/*     */ import java.text.SimpleDateFormat;
/*     */ import java.util.Date;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.UUID;
/*     */ import main.java.com.UpYun;
/*     */ import net.spy.memcached.MemcachedClient;
/*     */ import org.apache.commons.codec.digest.DigestUtils;
/*     */ import org.apache.commons.io.FileUtils;
/*     */ import org.apache.logging.log4j.LogManager;
/*     */ import org.apache.logging.log4j.Logger;
/*     */ import org.springframework.beans.factory.annotation.Autowired;
/*     */ import org.springframework.scheduling.annotation.EnableScheduling;
/*     */ import org.springframework.scheduling.annotation.Scheduled;
/*     */ import org.springframework.stereotype.Component;
/*     */ 
/*     */ @Component
/*     */ @EnableScheduling
/*     */ public class PublishWeixinArticlesTask extends BaseTask
/*     */ {
/*  34 */   private static final Logger LOGGER = LogManager.getLogger(PublishWeixinArticlesTask.class);
/*     */ 
/*     */   @Autowired
/*     */   private IArticles articlesService;
/*     */ 
/*     */   @Autowired
/*     */   private IArticlesContent articlesContentService;
/*     */ 
/*     */   @Autowired
/*     */   private IArticlesTags articlesTagsService;
/*     */ 
/*     */   @Autowired
/*     */   private MemcachedClient memcachedClient;
/*     */ 
/*     */   @Autowired
/*     */   private ISysConfig sysConfigService;
/*     */ 
/*  49 */   @Scheduled(cron="*/5 * * * * *")
/*     */   public void run() { super.run(); }
/*     */ 
/*     */ 
/*     */   public void doTask()
/*     */   {
/*  57 */     List openIds = (List)this.memcachedClient.get(new StringBuilder().append(MemcacheKeys.WEIXIN_RESOURCES.getKey()).append("openids").toString());
/*  58 */     if ((openIds == null) || (openIds.isEmpty())) {
/*  59 */       return;
/*     */     }
/*  61 */     String openid = (String)openIds.get(0);
/*  62 */     List<Map> resources = (List)this.memcachedClient.get(new StringBuilder().append(MemcacheKeys.WEIXIN_RESOURCES.getKey()).append(openid).toString());
/*  63 */     if ((resources == null) || (resources.isEmpty())) {
/*  64 */       openIds.remove(openid);
/*  65 */       this.memcachedClient.set(new StringBuilder().append(MemcacheKeys.WEIXIN_RESOURCES.getKey()).append("openids").toString(), 86400, openIds);
/*  66 */       return;
/*     */     }
/*  68 */     StringBuilder builder = new StringBuilder();
/*  69 */     File fileTmp = null;
/*  70 */     String indexPic = null;
/*  71 */     for (Map resource : resources) {
/*     */       try {
/*  73 */         URL url = new URL(new StringBuilder().append("http://api.weixin.qq.com/cgi-bin/media/get?access_token=").append(StaticProp.WX_TOKEN).append("&media_id=").append((String)resource.get("mediaId")).toString());
/*     */ 
/*  76 */         fileTmp = File.createTempFile(DigestUtils.md5Hex(UUID.randomUUID().toString()), null);
/*     */ 
/*  79 */         FileUtils.copyURLToFile(url, fileTmp);
/*     */       } catch (Exception e) {
/*  81 */         LOGGER.error("下载微信素材错误：", e);
/*  82 */       }continue;
/*     */ 
/*  85 */       String filePath = new StringBuilder().append(StaticProp.upYunPath).append(DigestUtils.md5Hex(UUID.randomUUID().toString())).toString();
/*  86 */       if ("image".equals(resource.get("type"))) {
/*  87 */         filePath = new StringBuilder().append(filePath).append(".jpg").toString();
/*  88 */         if (indexPic == null) {
/*  89 */           indexPic = new StringBuilder().append("http://img.s2m.cc/").append(filePath).toString();
/*     */         }
/*     */       }
/*  92 */       if ("video".equals(resource.get("type"))) {
/*  93 */         filePath = new StringBuilder().append(filePath).append(".mp4").toString();
/*     */       }
/*  95 */       if ("shortvideo".equals(resource.get("type")))
/*  96 */         filePath = new StringBuilder().append(filePath).append(".mp4").toString();
/*     */       try
/*     */       {
/*  99 */         boolean a = StaticProp.upyun.writeFile(filePath, fileTmp, true);
/* 100 */         if (!a)
/* 101 */           continue;
/*     */       }
/*     */       catch (Exception e) {
/* 104 */         LOGGER.error("上传至云服务器错误：", e);
/* 105 */       }continue;
/*     */ 
/* 107 */       filePath = new StringBuilder().append("http://img.s2m.cc/").append(filePath).toString();
/* 108 */       if ("image".equals(resource.get("type"))) {
/* 109 */         builder.append(new StringBuilder().append("<p><img src=\"").append(filePath).append("\"></p>").toString());
/*     */       }
/* 111 */       if ("video".equals(resource.get("type"))) {
/* 112 */         builder.append("<link href=\"/assets/video-js/video-js.css\" rel=\"stylesheet\" type=\"text/css\">");
/* 113 */         builder.append("<script src=\"/assets/video-js/video.js\"></script>");
/* 114 */         builder.append("<script>");
/* 115 */         builder.append("videojs.options.flash.swf = \"/assets/video-js/video-js.swf\";");
/* 116 */         builder.append("</script>");
/* 117 */         builder.append("<video class=\"video-js vjs-default-skin\" controls preload=\"none\" data-setup=\"{}\">");
/*     */ 
/* 120 */         builder.append(new StringBuilder().append("<source src=\"").append(filePath).append("\" type='video/mp4' />").toString());
/* 121 */         builder.append("<track kind=\"captions\" src=\"/assets/video-js/demo.captions.vtt\" srclang=\"en\" label=\"English\"></track>");
/* 122 */         builder.append("<track kind=\"subtitles\" src=\"/assets/video-js/demo.captions.vtt\" srclang=\"en\" label=\"English\"></track>");
/* 123 */         builder.append("<p class=\"vjs-no-js\">To view this video please enable JavaScript, and consider upgrading to a web browser that </p>");
/* 124 */         builder.append("</video>");
/*     */       }
/* 126 */       if ("shortvideo".equals(resource.get("type"))) {
/* 127 */         builder.append("<link href=\"/assets/video-js/video-js.css\" rel=\"stylesheet\" type=\"text/css\">");
/* 128 */         builder.append("<script src=\"/assets/video-js/video.js\"></script>");
/* 129 */         builder.append("<script>");
/* 130 */         builder.append("videojs.options.flash.swf = \"/assets/video-js/video-js.swf\";");
/* 131 */         builder.append("</script>");
/* 132 */         builder.append("<video class=\"video-js vjs-default-skin\" controls preload=\"none\" data-setup=\"{}\">");
/*     */ 
/* 135 */         builder.append(new StringBuilder().append("<source src=\"").append(filePath).append("\" type='video/mp4' />").toString());
/* 136 */         builder.append("<track kind=\"captions\" src=\"/assets/video-js/demo.captions.vtt\" srclang=\"en\" label=\"English\"></track>");
/* 137 */         builder.append("<track kind=\"subtitles\" src=\"/assets/video-js/demo.captions.vtt\" srclang=\"en\" label=\"English\"></track>");
/* 138 */         builder.append("<p class=\"vjs-no-js\">To view this video please enable JavaScript, and consider upgrading to a web browser that </p>");
/* 139 */         builder.append("</video>");
/*     */       }
/*     */     }
/* 142 */     Articles art = new Articles();
/* 143 */     art.setAuthor("S2M");
/* 144 */     art.setProfile("分享至微信公众号：");
/* 145 */     art.setTitle(new StringBuilder().append("分享自微信：").append(new SimpleDateFormat("yyyy-MM-dd").format(new Date())).toString());
/* 146 */     art.setUrl("wxsssssss");
/* 147 */     if (indexPic != null) {
/* 148 */       art.setPic(indexPic);
/*     */     }
/* 150 */     this.articlesService.insertSelective(art);
/* 151 */     art.setUrl(new StringBuilder().append("wx").append(art.getId()).toString());
/* 152 */     this.articlesService.updateByPrimaryKeySelective(art);
/* 153 */     ArticlesContent content = new ArticlesContent();
/* 154 */     content.setId(art.getId());
/* 155 */     content.setContent(builder.toString());
/* 156 */     this.articlesContentService.insertSelective(content);
/* 157 */     ArticlesTags articlesTags = new ArticlesTags();
/* 158 */     articlesTags.setAid(art.getId());
/* 159 */     articlesTags.setTid(Integer.valueOf(Integer.parseInt(this.sysConfigService.getByCode("WEIXIN_TAG").getValue())));
/* 160 */     this.articlesTagsService.insertSelective(articlesTags);
/* 161 */     this.memcachedClient.delete(new StringBuilder().append(MemcacheKeys.WEIXIN_RESOURCES.getKey()).append(openid).toString());
/* 162 */     openIds.remove(openid);
/* 163 */     this.memcachedClient.set(new StringBuilder().append(MemcacheKeys.WEIXIN_RESOURCES.getKey()).append("openids").toString(), 86400, openIds);
/*     */   }
/*     */ }

/* Location:           C:\Users\Administrator\Desktop\s2mBlog.zip
 * Qualified Name:     s2mBlog-3.0.0.WEB-INF.classes.cc.s2m.web.s2mBlog.task.PublishWeixinArticlesTask
 * JD-Core Version:    0.6.1
 */